//
//  LuFoodController.m
//  DongChiDaChiProject1
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "LuFoodController.h"
#import "UserManager.h"

@interface LuFoodController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *dataArray;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) NSInteger pnumber;

@end

@implementation LuFoodController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.title = @"鲁菜";
    // 调用表格
    [self addSubViews];
   
    [self searchNet];
    self.view.backgroundColor = [[UIColor orangeColor]colorWithAlphaComponent:0.2];
  //  [self.navigationController.navigationBar setBarTintColor:[UIColor orangeColor]];
    [self addBar];
    
}

#pragma mark - 添加网络检测
- (void)searchNet
{
    NSLog(@"wangluo = %@", [[UserManager shareUserManagerHandle] getNet]);
    
    if ([[[UserManager shareUserManagerHandle] getNet] isEqualToString:@"NO"]) {
        // 网络不通
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:@"该软件只有在网络状态下才能使用" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVC addAction:alertAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    } else {
        // 调用刷新
        [self setUp];
    }
}

#pragma mark - 视图将要出现
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

#pragma mark - 添加bar
- (void)addBar
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(returnToLast:)];
    leftItem.tintColor = [UIColor colorWithHue:0.8 saturation:1 brightness:0.7 alpha:1];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    [self.navigationController.navigationBar setTranslucent:NO];
}

- (void)returnToLast:(UIBarButtonItem *)barItem
{
    [self.navigationController popViewControllerAnimated:YES];
}

// 建立下拉刷新
- (void)setUp
{
    // 下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    // 脚码
    self.tableView.footer = [MJRefreshBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    // 进入刷新状态
    [self.tableView.header beginRefreshing];
}
// 表头下拉刷新方法
- (void)headerRefresh
{
    // 在这个方法中请求数据
    self.pnumber++;
    if (self.pnumber > 8) {
        self.pnumber = 1;
    }
    
    NSString *str1 = @"http://ibaby.ipadown.com/api/food/food.show.list.php?category=%E9%B2%81%E8%8F%9C&&p=";
    
    NSString *str2 = [NSString stringWithFormat:@"%@%ld&pagesize=12",str1, self.pnumber];

    // 调用加载数据
    [[EightFoodManager shareManager] requestDataForReloadWithBlock:^(NSMutableArray *array) {
        //传值进来
        self.dataArray = [NSMutableArray arrayWithArray:array];
        [self.tableView reloadData];
        
    } url:str2 pNumber:1];
    // 结束刷新状态
    [self.tableView.header endRefreshing];
    NSLog(@"刷新了");
}
// 表尾上啦刷新状态
- (void)footerRefresh
{
    NSLog(@"下拉");
    [self.tableView.footer beginRefreshing];
    // 结束刷新状态
    [self.tableView.footer endRefreshing];
}

// 添加表视图
- (void)addSubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    // 取消下划线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    self.tableView.tableFooterView = footView;
    [self.view addSubview:self.tableView];
    
}
// 表格的代理方法
// 返回分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}
// 分区多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

// 设置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"myCell";
    LuFoodCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[LuFoodCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    //    EightFoodModel *model = [[EightFoodManager shareManager] getEightFoodModelAtIndex:indexPath.row];
    cell.model = [[EightFoodManager shareManager] getEightFoodModelAtIndex:indexPath.row];
    cell.caixiLable.text = @"鲁菜";
    
    return cell;
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 170 * kWidthFitParameter;
}
// cell 的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    LuCaiController *luCaiVC = [[LuCaiController alloc] init];
    EightFoodModel *model = self.dataArray[indexPath.row];
    luCaiVC.model = model;
    [self.navigationController pushViewController:luCaiVC animated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
